package com.mock.action.organisation.directorate;

import com.mock.action.AbstractAction;
import com.mock.dao.DirectorateDao;
import com.mock.dao.impl.DirectorateDaoImp;
import com.mock.model.Address;
import com.mock.model.Contact;
import com.mock.model.DataTable;
import com.mock.model.Directorate;

import java.util.ArrayList;
import java.util.List;
public class DirectorateAction extends AbstractAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = -8118749358737241847L;

	
	public String directorateAjaxTable(){
		
		DataTable dt = new DataTable();
		dt.sEcho = sEcho = getRequest().getParameter("sEcho");
		dt.iDisplayStart = iDisplayStart = Integer.parseInt(getRequest()
				.getParameter("iDisplayStart"));
		dt.iDisplayLength = iDisplayLength = Integer.parseInt(getRequest()
				.getParameter("iDisplayLength"));
		dt.sSearch_0 = sSearch_0 = getRequest().getParameter("sSearch_0");		
		dt.sSearch_4 = sSearch_4 = getRequest().getParameter("sSearch_4");		
		dt.iSortCol_0 = iSortCol_0 = Integer.parseInt(getRequest()
				.getParameter("iSortCol_0"));
		dt.sSortDir_0 = sSortDir_0 = getRequest().getParameter("sSortDir_0");
		
		dt.iTotalDisplayRecords = iTotalDisplayRecords = directorateDao.getCountRow();

		organisationId = Long.parseLong(getRequest().getParameter("organisationId"));
		
		List<Directorate> listDirectorates = directorateDao.getDirectorateByQuery(dt, organisationId);
		for(Directorate directorate : listDirectorates){
			ArrayList<String> arrayList = new ArrayList<String>();
			arrayList.add(directorate.getDirectorateName());
			arrayList.add(directorate.getAddressName1());
			arrayList.add(directorate.getPostcode());
			arrayList.add(directorate.getContact().getFirstname() + " " + directorate.getContact().getSurname());
			
			if(directorate.isActive()){
				arrayList.add("Yes");
			}else{
				arrayList.add("No");
			}
			arrayList.add(Long.toString(directorate.getDirectorateId()));
			arrayList.add(Long.toString(directorate.getOrganisation().getOrganisationId()));
			aaData.add(arrayList);
		}
		return SUCCESS;
	}

	public Long getOrganisationId() {
		return organisationId;
	}

	public void setOrganisationId(Long organisationId) {
		this.organisationId = organisationId;
	}

	public String setActive(){
		
		directorateId = Long.valueOf(getRequest().getParameter("directorateId"));
		DirectorateDao directorateDao = new DirectorateDaoImp();
		Directorate directorate = directorateDao.getById(directorateId);
		directorate.setActive(true);
		organisationId = directorate.getOrganisation().getOrganisationId();
		directorateDao.update(directorate);
		return SUCCESS;
	}
	private DirectorateDao directorateDao = new DirectorateDaoImp();

	
	private List<ArrayList<String>> aaData = new ArrayList<ArrayList<String>>();
	private long directorateId = -1L;
	private String sEcho;
	private String sSearch;// gan cot bang Yes, khi load chi hien thi gia tri Yes
	private String sSearch_0;
	private int iDisplayLength;
	private int iDisplayStart;
	private int iTotalRecords;
	private int iTotalDisplayRecords;
	private int iSortCol_0;
	private String sSortDir_0;
	private String sSearch_4;
	Long organisationId;
	
	// getter and setter
	public long getDirectorateId() {
		return directorateId;
	}
	public void setDirectorateId(long directorateId) {
		this.directorateId = directorateId;
	}
	public List<ArrayList<String>> getAaData() {
		return aaData;
	}
	public void setAaData(List<ArrayList<String>> aaData) {
		this.aaData = aaData;
	}
	public String getsEcho() {
		return sEcho;
	}
	public void setsEcho(String sEcho) {
		this.sEcho = sEcho;
	}
	public String getsSearch() {
		return sSearch;
	}
	public void setsSearch(String sSearch) {
		this.sSearch = sSearch;
	}
	public int getiDisplayLength() {
		return iDisplayLength;
	}
	public void setiDisplayLength(int iDisplayLength) {
		this.iDisplayLength = iDisplayLength;
	}
	public int getiDisplayStart() {
		return iDisplayStart;
	}
	public void setiDisplayStart(int iDisplayStart) {
		this.iDisplayStart = iDisplayStart;
	}
	public int getiTotalRecords() {
		return iTotalRecords;
	}
	public void setiTotalRecords(int iTotalRecords) {
		this.iTotalRecords = iTotalRecords;
	}
	public int getiTotalDisplayRecords() {
		return iTotalDisplayRecords;
	}
	public void setiTotalDisplayRecords(int iTotalDisplayRecords) {
		this.iTotalDisplayRecords = iTotalDisplayRecords;
	}
	public String getsSearch_0() {
		return sSearch_0;
	}
	public void setsSearch_0(String sSearch_0) {
		this.sSearch_0 = sSearch_0;
	}
	public int getiSortCol_0() {
		return iSortCol_0;
	}
	public void setiSortCol_0(int iSortCol_0) {
		this.iSortCol_0 = iSortCol_0;
	}
	public String getsSortDir_0() {
		return sSortDir_0;
	}
	public void setsSortDir_0(String sSortDir_0) {
		this.sSortDir_0 = sSortDir_0;
	}
	public String getsSearch_4() {
		return sSearch_4;
	}
	public void setsSearch_4(String sSearch_4) {
		this.sSearch_4 = sSearch_4;
	}
	
	
	
	
	
}
